/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.ldap.BySearchResultEntrySearcher;
/*     */ import com.zimbra.cs.account.ldap.BySearchResultEntrySearcher.SearchEntryProcessor;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProvisioning;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEntry;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class DistributionList
/*     */   extends ZAttrDistributionList
/*     */   implements GroupedEntry
/*     */ {
/*     */   protected static final String MEMBER_ATTR = "zimbraMailForwardingAddress";
/*     */   
/*     */   public DistributionList(String name, String id, Map<String, Object> attrs, Provisioning prov)
/*     */   {
/*  49 */     super(name, id, attrs, prov);
/*     */   }
/*     */   
/*     */   public Entry.EntryType getEntryType()
/*     */   {
/*  54 */     return Entry.EntryType.DISTRIBUTIONLIST;
/*     */   }
/*     */   
/*     */   public boolean isDynamic()
/*     */   {
/*  59 */     return false;
/*     */   }
/*     */   
/*     */   public Domain getDomain() throws ServiceException
/*     */   {
/*  64 */     return getProvisioning().getDomain(this);
/*     */   }
/*     */   
/*     */   public void modify(Map<String, Object> attrs) throws ServiceException {
/*  68 */     getProvisioning().modifyAttrs(this, attrs);
/*     */   }
/*     */   
/*     */   public void deleteDistributionList() throws ServiceException {
/*  72 */     getProvisioning().deleteDistributionList(getId());
/*     */   }
/*     */   
/*     */   public void addAlias(String alias) throws ServiceException {
/*  76 */     getProvisioning().addAlias(this, alias);
/*     */   }
/*     */   
/*     */   public void removeAlias(String alias) throws ServiceException {
/*  80 */     getProvisioning().removeAlias(this, alias);
/*     */   }
/*     */   
/*     */   public void renameDistributionList(String newName) throws ServiceException {
/*  84 */     getProvisioning().renameDistributionList(getId(), newName);
/*     */   }
/*     */   
/*     */   public void addMembers(String[] members) throws ServiceException {
/*  88 */     getProvisioning().addMembers(this, members);
/*     */   }
/*     */   
/*     */   public void removeMembers(String[] member) throws ServiceException {
/*  92 */     getProvisioning().removeMembers(this, member);
/*     */   }
/*     */   
/*     */   public String[] getAllMembers() throws ServiceException
/*     */   {
/*  97 */     return getMultiAttr("zimbraMailForwardingAddress");
/*     */   }
/*     */   
/*     */   public Set<String> getAllMembersSet() throws ServiceException
/*     */   {
/* 102 */     return getMultiAttrSet("zimbraMailForwardingAddress");
/*     */   }
/*     */   
/*     */   public String[] getAliases() throws ServiceException
/*     */   {
/* 107 */     return getMultiAttr("zimbraMailAlias");
/*     */   }
/*     */   
/*     */   protected void resetData()
/*     */   {
/* 112 */     super.resetData();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String[] getAllAddrsAsGroupMember()
/*     */     throws ServiceException
/*     */   {
/* 120 */     String[] aliases = getAliases();
/* 121 */     List<String> addrs = Lists.newArrayListWithExpectedSize(aliases.length + 1);
/* 122 */     String myName = getName();
/* 123 */     addrs.add(myName);
/* 124 */     for (String alias : aliases)
/*     */     {
/* 126 */       if (!alias.equals(myName)) {
/* 127 */         addrs.add(alias);
/*     */       }
/*     */     }
/* 130 */     return (String[])addrs.toArray(new String[0]);
/*     */   }
/*     */   
/*     */   private static final class BasicInfo {
/*     */     final String id;
/*     */     final String name;
/*     */     final String[] aliases;
/*     */     final boolean isAdmin;
/*     */     
/* 139 */     BasicInfo(String id, String mail, String[] aliases, boolean isAdmin) { this.id = id;
/* 140 */       this.isAdmin = isAdmin;
/* 141 */       this.name = mail;
/* 142 */       this.aliases = aliases;
/*     */     }
/*     */     
/*     */     BasicInfo(DistributionList dl) throws ServiceException {
/* 146 */       this.id = dl.getId();
/* 147 */       this.isAdmin = dl.isIsAdminGroup();
/* 148 */       this.name = dl.getName();
/* 149 */       this.aliases = dl.getAllAddrsAsGroupMember();
/*     */     }
/*     */     
/*     */     public List<String> getAllAddrsAsGroupMember() throws ServiceException {
/* 153 */       List<String> addrs = Lists.newArrayListWithExpectedSize(this.aliases.length + 1);
/* 154 */       addrs.add(this.name);
/* 155 */       for (String alias : this.aliases) {
/* 156 */         if (!alias.equals(this.name)) {
/* 157 */           addrs.add(alias);
/*     */         }
/*     */       }
/* 160 */       return addrs;
/*     */     }
/*     */     
/* 163 */     public Provisioning.MemberOf toMemberOf() { return new Provisioning.MemberOf(this.id, this.isAdmin, false); }
/*     */     
/*     */     public static Provisioning.GroupMembership mergeIntoGroupMembership(Provisioning.GroupMembership membership, List<BasicInfo> dls) {
/* 166 */       for (BasicInfo dl : dls) {
/* 167 */         membership.append(dl.toMemberOf());
/*     */       }
/* 169 */       return membership;
/*     */     }
/*     */   }
/*     */   
/* 173 */   private static final Set<SearchDirectoryOptions.ObjectType> DISTRIBUTION_LISTS = Sets.newHashSet(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.distributionlists });
/* 174 */   private static final String[] BASIC_ATTRS = { "zimbraId", "zimbraIsAdminGroup", "zimbraMailAlias" };
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Provisioning.GroupMembership updateGroupMembership(LdapProvisioning prov, ZLdapContext zlc, Provisioning.GroupMembership membership, Account acct, Map<String, String> via, boolean adminGroupsOnly, boolean directOnly)
/*     */     throws ServiceException
/*     */   {
/* 185 */     boolean ownContext = false;
/* 186 */     String[] addrs = acct.getAllAddrsAsGroupMember();
/* 187 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().distributionListsByMemberAddrs(addrs);
/* 188 */     ContainingDLUpdator dlUpdator = new ContainingDLUpdator(prov, adminGroupsOnly);
/*     */     try {
/* 190 */       if (zlc == null) {
/* 191 */         ownContext = true;
/* 192 */         zlc = LdapClient.getContext(LdapServerType.get(false), LdapUsage.SEARCH);
/*     */       }
/* 194 */       BySearchResultEntrySearcher searcher = new BySearchResultEntrySearcher(prov, zlc, (Domain)null, BASIC_ATTRS, dlUpdator);
/*     */       
/* 196 */       searcher.doSearch(filter, DISTRIBUTION_LISTS);
/* 197 */       List<BasicInfo> directDLs = dlUpdator.getDistLists();
/* 198 */       BasicInfo.mergeIntoGroupMembership(membership, directDLs);
/* 199 */       if (directOnly)
/* 200 */         return membership;
/*     */       Object i$;
/* 202 */       if (via == null) {
/* 203 */         updateGroupMembership(prov, zlc, membership, directDLs, adminGroupsOnly, directOnly);
/*     */       } else {
/* 205 */         for (i$ = directDLs.iterator(); ((Iterator)i$).hasNext();) { BasicInfo directDL = (BasicInfo)((Iterator)i$).next();
/* 206 */           updateGroupMembership(prov, zlc, membership, directDL, via, adminGroupsOnly, directOnly);
/*     */         }
/*     */       }
/* 209 */       return membership;
/*     */     } finally {
/* 211 */       if (ownContext) {
/* 212 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static Provisioning.GroupMembership updateGroupMembership(LdapProvisioning prov, ZLdapContext zlc, Provisioning.GroupMembership membership, DistributionList dl, Map<String, String> via, boolean adminGroupsOnly, boolean directOnly)
/*     */     throws ServiceException
/*     */   {
/* 221 */     return updateGroupMembership(prov, zlc, membership, new BasicInfo(dl), via, adminGroupsOnly, directOnly);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static Provisioning.GroupMembership updateGroupMembership(LdapProvisioning prov, ZLdapContext zlc, Provisioning.GroupMembership membership, BasicInfo dl, Map<String, String> via, boolean adminGroupsOnly, boolean directOnly)
/*     */     throws ServiceException
/*     */   {
/* 232 */     boolean ownContext = false;
/*     */     try {
/* 234 */       if (zlc == null) {
/* 235 */         ownContext = true;
/* 236 */         zlc = LdapClient.getContext(LdapServerType.get(false), LdapUsage.SEARCH);
/*     */       }
/* 238 */       List<BasicInfo> directDLs = getContainingDLs(prov, zlc, dl, adminGroupsOnly, true);
/* 239 */       Iterator<BasicInfo> iter = directDLs.iterator();
/* 240 */       BasicInfo directDL; while (iter.hasNext()) {
/* 241 */         directDL = (BasicInfo)iter.next();
/* 242 */         if (membership.groupIds().contains(directDL.id)) {
/* 243 */           iter.remove();
/* 244 */         } else if ((via != null) && (membership.groupIds().contains(dl.id))) {
/* 245 */           via.put(directDL.name, dl.name);
/*     */         }
/*     */       }
/* 248 */       BasicInfo.mergeIntoGroupMembership(membership, directDLs);
/* 249 */       if (directOnly) {
/* 250 */         return membership;
/*     */       }
/* 252 */       if (via == null) {
/* 253 */         updateGroupMembership(prov, zlc, membership, directDLs, adminGroupsOnly, directOnly);
/*     */       } else {
/* 255 */         for (BasicInfo directDL : directDLs) {
/* 256 */           updateGroupMembership(prov, zlc, membership, directDL, via, adminGroupsOnly, directOnly);
/*     */         }
/*     */       }
/* 259 */       return membership;
/*     */     } finally {
/* 261 */       if (ownContext) {
/* 262 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static Provisioning.GroupMembership updateGroupMembership(LdapProvisioning prov, ZLdapContext zlc, Provisioning.GroupMembership membership, List<BasicInfo> dls, boolean adminGroupsOnly, boolean directOnly)
/*     */     throws ServiceException
/*     */   {
/* 271 */     boolean ownContext = false;
/*     */     try {
/* 273 */       if (zlc == null) {
/* 274 */         ownContext = true;
/* 275 */         zlc = LdapClient.getContext(LdapServerType.get(false), LdapUsage.SEARCH);
/*     */       }
/* 277 */       List<BasicInfo> directDLs = getContainingDLs(prov, zlc, dls, adminGroupsOnly, true);
/* 278 */       Iterator<BasicInfo> iter = directDLs.iterator();
/* 279 */       BasicInfo directDL; while (iter.hasNext()) {
/* 280 */         directDL = (BasicInfo)iter.next();
/* 281 */         if (membership.groupIds().contains(directDL.id)) {
/* 282 */           iter.remove();
/*     */         }
/*     */       }
/* 285 */       BasicInfo.mergeIntoGroupMembership(membership, directDLs);
/* 286 */       if ((directOnly) || (directDLs.isEmpty())) {
/* 287 */         return membership;
/*     */       }
/* 289 */       updateGroupMembership(prov, zlc, membership, directDLs, adminGroupsOnly, directOnly);
/* 290 */       return membership;
/*     */     } finally {
/* 292 */       if (ownContext) {
/* 293 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static List<BasicInfo> getContainingDLs(LdapProvisioning prov, ZLdapContext zlc, BasicInfo dl, boolean adminGroupsOnly, boolean directOnly)
/*     */     throws ServiceException
/*     */   {
/* 301 */     String[] addrs = (String[])dl.getAllAddrsAsGroupMember().toArray(new String[0]);
/* 302 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().distributionListsByMemberAddrs(addrs);
/* 303 */     ContainingDLUpdator dlUpdator = new ContainingDLUpdator(prov, adminGroupsOnly);
/* 304 */     BySearchResultEntrySearcher searcher = new BySearchResultEntrySearcher(prov, zlc, (Domain)null, BASIC_ATTRS, dlUpdator);
/*     */     
/* 306 */     searcher.doSearch(filter, DISTRIBUTION_LISTS);
/* 307 */     return dlUpdator.getDistLists();
/*     */   }
/*     */   
/*     */   public static List<BasicInfo> getContainingDLs(LdapProvisioning prov, ZLdapContext zlc, List<BasicInfo> dls, boolean adminGroupsOnly, boolean directOnly)
/*     */     throws ServiceException
/*     */   {
/* 313 */     int chunkSize = 500;
/* 314 */     List<String> addrs = Lists.newArrayList();
/* 315 */     for (BasicInfo dl : dls) {
/* 316 */       addrs.addAll(dl.getAllAddrsAsGroupMember());
/*     */     }
/* 318 */     int lastIndex = addrs.size() - 1;
/* 319 */     int start = 0;
/* 320 */     int end = lastIndex < 500 ? lastIndex : 499;
/* 321 */     List<BasicInfo> containingDLs = Lists.newArrayList();
/* 322 */     while (end <= lastIndex) {
/* 323 */       String[] chunk = (String[])addrs.subList(start, end + 1).toArray(new String[0]);
/* 324 */       ZLdapFilter filter = ZLdapFilterFactory.getInstance().distributionListsByMemberAddrs(chunk);
/* 325 */       ContainingDLUpdator dlUpdator = new ContainingDLUpdator(prov, adminGroupsOnly);
/* 326 */       BySearchResultEntrySearcher searcher = new BySearchResultEntrySearcher(prov, zlc, (Domain)null, BASIC_ATTRS, dlUpdator);
/*     */       
/* 328 */       searcher.doSearch(filter, DISTRIBUTION_LISTS);
/* 329 */       containingDLs.addAll(dlUpdator.getDistLists());
/* 330 */       if (end >= lastIndex) {
/*     */         break;
/*     */       }
/* 333 */       start += 500;
/* 334 */       end += 500;
/* 335 */       if (end > lastIndex) {
/* 336 */         end = lastIndex;
/*     */       }
/*     */     }
/* 339 */     return containingDLs;
/*     */   }
/*     */   
/*     */   private static class ContainingDLUpdator implements BySearchResultEntrySearcher.SearchEntryProcessor {
/*     */     private final boolean adminGroupsOnly;
/*     */     private final LdapProvisioning ldapProv;
/* 345 */     private final List<DistributionList.BasicInfo> distLists = Lists.newArrayList();
/*     */     
/*     */     public ContainingDLUpdator(LdapProvisioning ldapProv, boolean adminGroupsOnly) {
/* 348 */       this.ldapProv = ldapProv;
/* 349 */       this.adminGroupsOnly = adminGroupsOnly;
/*     */     }
/*     */     
/*     */     public void processSearchEntry(ZSearchResultEntry sr)
/*     */     {
/* 354 */       ZAttributes attrs = sr.getAttributes();
/*     */       try {
/* 356 */         String id = attrs.getAttrString("zimbraId");
/* 357 */         String name = this.ldapProv.getDIT().dnToEmail(sr.getDN(), attrs);
/* 358 */         String isAdminStr = attrs.getAttrString("zimbraIsAdminGroup");
/* 359 */         String[] aliases = attrs.getMultiAttrString("zimbraMailAlias");
/* 360 */         boolean isAdmin = isAdminStr == null ? false : "TRUE".equals(isAdminStr);
/* 361 */         if ((this.adminGroupsOnly) && (!isAdmin)) {
/* 362 */           return;
/*     */         }
/* 364 */         this.distLists.add(new DistributionList.BasicInfo(id, name, aliases, isAdmin));
/*     */       } catch (ServiceException e) {
/* 366 */         ZimbraLog.search.debug("Problem processing search result entry - ignoring", e);
/* 367 */         return;
/*     */       }
/*     */     }
/*     */     
/*     */     public List<DistributionList.BasicInfo> getDistLists() {
/* 372 */       return this.distLists;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/DistributionList.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */